<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-if="userType == 'all'"
        v-model="listQuery.proxy"
        placeholder="请选择代理"
        clearable
        filterable
        style="width: 150px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option
          v-for="(item, index) in dailiArr"
          :key="index"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-date-picker
        v-model="value1"
        align="left"
        unlink-panels
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item pickertime"
        format="yyyy-MM-dd"
        value-format="timestamp"
        :picker-options="pickerOptions2"
        @change="handleFilter"
      />
      <template>
        <el-select
          v-model="listQuery.status"
          placeholder="请选择状态"
          clearable
          filterable
          style="width: 150px"
          class="filter-item"
          @change="handleFilter"
        >
          <el-option label="全部" value="" />
          <el-option label="已结算" value="1" />
          <el-option label="未结算" value="2" />
          <el-option label="已作废" value="3" />
        </el-select>
      </template>
      <a-button type="primary" size="large" @click="handleFilter">
        搜索
      </a-button>
      <!-- <el-button v-waves class="filter-item" plain size="small" type="primary" icon="el-icon-refresh" @click="handleCancel">
        重置
      </el-button> -->
      <span style="margin-left: 20px" />
    </div>

    <el-table
      v-loading="listLoading"
      fit
      :key="tableKey"
      :data="list"
      :summary-method="getSummaries"
      highlight-current-row
      style="width: 100%"
    >
      <!-- <el-table-column label="日期" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.time_ }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="结算日期" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.period }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" min-width="80" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="UV" min-width="100" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.uv }}</span>
        </template>
      </el-table-column>
      <el-table-column label="PV" min-width="100" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.pv }}</span>
        </template>
      </el-table-column>
      <el-table-column label="万次UV单价" min-width="100" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" min-width="100" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="80" align="center">
        <template slot-scope="{ row }">
          <!-- <el-link :type="row.status | statusFilter"> -->
          {{ allStatus[row.status] }}
          <!-- </el-link> -->
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        min-width="180"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <a-button
            type="link"
            :disabled="row.status != 2"
            size="small"
            @click="handleSettle(row, '结算')"
          >
            结算
          </a-button>
          <a-button
            type="link"
            size="small"
            :disabled="row.status != 2"
            @click="handleSettle(row, '删除')"
          >
            删除
          </a-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      class="pageShow"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.page_size"
      @pagination="getList"
    />
    <pagination
      v-show="total > 0"
      layout="prev, pager, next"
      :small="true"
      class="pageHidden"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.page_size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getSettleList,
  getProxyNameList,
  changeSettleStatus,
} from "@/api/admin/admin";

import { fetchList, settleMoney, del } from "@/api/admin/settle";
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { MessageBox, Message } from "element-ui";

export default {
  name: "Settle",
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: "success",
        0: "danger",
        2: "info",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      userType: this.$store.state.user.type,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      value1: "",
      countPv: "",
      countUv: "",
      countProfit: "",
      allStatus: {
        1: "已结算",
        2: "未结算",
        3: "已作废",
      },
      dailiArr: [],
      pickerOptions2: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      listQuery: {
        page: 1,
        page_size: 20,
        proxy: "",
        time_start: "",
        time_end: "",
        status: "",
      },
    };
  },
  created() {
    this.getProxyNameList();
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      if (this.value1) {
        this.listQuery.time_start = this.value1[0] / 1000;
        this.listQuery.time_end = this.value1[1] / 1000;
      } else {
        this.listQuery.time_start = "";
        this.listQuery.time_end = "";
      }
      getSettleList(this.listQuery).then((response) => {
        this.list = response.DATA.list;
        this.total = response.DATA.total;
        this.countPv = response.DATA.sum.pv;
        this.countUv = response.DATA.sum.uv;
        this.countProfit = response.DATA.sum.money;
        this.listLoading = false;
      });
    },

    //获取代理列表
    getProxyNameList() {
      getProxyNameList().then((r) => {
        this.dailiArr = r.DATA;
      });
    },
    getSummaries() {
      const sums = [
        "总计",
        "-",
        "-",
        this.countUv,
        this.countPv,
        "-",
        this.countProfit,
        "-",
        "-",
      ];
      return sums;
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleCancel() {
      this.value1 = "";
      this.listQuery.page = 1;
      this.listQuery.proxy = "";
      this.listQuery.status = "";
      this.getList();
    },
    handleSettle(row, title) {
      MessageBox.confirm(`确定${title}吗？`, "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let obj = {
            id: row.id,
            status: title == "结算" ? 1 : 3,
          };

          changeSettleStatus(obj).then((response) => {
            if (response.STATUS === 200) {
              this.getList();
              Message({
                message: response.msg || `${title}成功`,
                type: "success",
                duration: 2000,
              });
            } else {
              Message({
                message: response.msg || `${title}成功`,
                type: "error",
                duration: 2000,
              });
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.pageHidden {
  display: none;
}
@media (max-width: 550px) {
  .pageShow {
    display: none;
  }
  .pageHidden {
    display: block;
  }
  .pickertime {
    width: 100%;
  }
}
.filter-item {
  margin-bottom: 3px;
}
</style>
